package com.project.common.pagination;

import com.fasterxml.jackson.annotation.JsonIgnore;

import java.util.ArrayList;
import java.util.List;

public final class PagedRecord<T> {
    private int pageIndex;
    private int pageSize;
    private long totalCount;
    private List<T> records;

    private PagedRecord(int pageIndex, int pageSize, long totalCount, List<T> records) {
        this.pageSize = pageSize;
        this.pageIndex = pageIndex;
        this.totalCount = totalCount;
        this.records = records;
    }

    public void setRecords(List<T> records) {
        this.records = records;
    }

    public static <R> PagedRecord<R> of(int pageIndex, int pageSize, long totalCount, List<R> records) {
        return new PagedRecord<>(pageIndex, pageSize, totalCount, records);
    }

    public static <R> PagedRecord<R> empty(int pageIndex, int pageSize) {
        return of(pageIndex, pageSize, 0, new ArrayList<>());
    }

    @JsonIgnore
    public boolean isEmpty() {
        return this.records == null || this.records.isEmpty();
    }

    public int getPageSize() {
        return pageSize;
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public long getTotalCount() {
        return totalCount;
    }

    public List<T> getRecords() {
        return records;
    }
}
